local skill = fk.CreateSkill {
  name = "lvshi__qice",
  tags = {"Turn"}
}

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["lvshi__qice"] = "奇策",
  ["#lvshi__qice"] = "奇策：请选择锦囊牌名称和目标角色",
  [":lvshi__qice"] = "回合技，你可将手牌数调整至1，视为使用一张未以此法使用过的通常锦囊牌。",
  ["lvshi__zhiyu"] = "智愚",
  [":lvshi__zhiyu"] = "当你受到伤害后，你可令两名角色拼点，拼点赢的角色可令你回复1点体力，否则你重置“奇策”的一个牌名。",
  ["#lvshi__ice_reset"] = "智愚",
  ["#lvshi__zhiyu-choose"] = "智愚：你可令两名角色拼点",
  ["#lvshi__zhiyu-recover"] = "智愚：你可令%src回复1点体力",
  ["#zhiyu-reset"] = "智愚：你可令%src重置“奇策”",

  ["$lvshi__qice1"] = "腹有经纶，到用时施无穷之计。",
  ["$lvshi__qice2"] = "胸纳甲兵，烽烟起可靖疆晏海。",
}

skill:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "qice_names", 0)
end)

skill:addEffect("viewas", {
  pattern = ".|.|.|.|.|trick",
  prompt = "#lvshi__qice",
  interaction = function(self, player)
    local all_names = {}
    if Fk.currentResponsePattern == "nullification" then
      table.insertIfNeed(all_names, "lvshi__nullification")
    else
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card:isCommonTrick() and not card.is_derived then
          table.insertIfNeed(all_names, card.name)
        end
      end
    end
    local names = table.simpleClone(all_names)
    local qicenames = player:getTableMark("qice_names")
    for _, name in ipairs(qicenames) do
      table.removeOne(names, name)
    end
    if #names == 0 then return false end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards ~= 0 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skill.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    room:addTableMark(player, "qice_names", use.card.name)
    if player:getHandcardNum() ~= 1 then
      local n = math.abs(player:getHandcardNum() - 1)
      if player:getHandcardNum() > 1 then
        player.room:askToDiscard(player, {
          skill_name = skill.name,
          cancelable = false,
          min_num = n,
          max_num = n,
          include_equip = false,
        })
      else
        player:drawCards(n, self.name)
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    if response then return false end
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
})

return skill